package com.tianqi.modules.pms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianqi.common.api.CommonResult;
import com.tianqi.modules.pms.dto.request.ConsultingFeeRequestDTO;
import com.tianqi.modules.pms.dto.response.ConsultingFeeResponseDTO;
import com.tianqi.modules.pms.mapper.PmsConsultingFeeMapper;
import com.tianqi.modules.pms.mapper.PmsSteelPriceMapper;
import com.tianqi.modules.pms.model.PmsConsultingFee;
import com.tianqi.modules.pms.model.PmsSteelPrice;
import com.tianqi.modules.pms.service.PmsConsultingFeeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 咨询费用计算依据表 服务实现类
 * </p>
 *
 * @author rory
 * @since 2022-10-04
 */
@Service
public class PmsConsultingFeeServiceImpl extends ServiceImpl<PmsConsultingFeeMapper, PmsConsultingFee> implements PmsConsultingFeeService {

	@Resource
	private PmsConsultingFeeMapper feeMapper;

	@Resource
	private PmsSteelPriceMapper steelPriceMapper;

	/**
	 * 咨询费计算
	 *
	 * @param requestDTO
	 * @return {@link CommonResult <  BigDecimal >}
	 * @author zhangzedi
	 * @date 2022/10/4 10:58
	 */
	@Override
	public CommonResult<ConsultingFeeResponseDTO> calculationFee(ConsultingFeeRequestDTO requestDTO) {
		//返回结果
		ConsultingFeeResponseDTO responseDTO = new ConsultingFeeResponseDTO();
		requestDTO.setAreaCode(requestDTO.getAreaCode());
		PmsConsultingFee param = new PmsConsultingFee();
		param.setAreaCode(requestDTO.getAreaCode());
		param.setChargeItem(requestDTO.getConsultType());
		List<PmsConsultingFee> result = feeMapper.selectConsulFeeListByCondition(param);
		//循环处理金额
		BigDecimal chargeFee = new BigDecimal("0");
		StringBuilder sb = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		for (PmsConsultingFee item : result) {
			//判断输入的标的金额和划分档位的最大值 的大小
			if (item.getGradedCumulativeEnd().compareTo(requestDTO.getTargetMoney()) < 0) {
				//判断最大市师傅大与等于100001万，是则说明为最后一档,最后一档直接用金额减去最小值然后去乘
				if (item.getGradedCumulativeEnd().compareTo(new BigDecimal("100001")) >= 0) {
					BigDecimal temp =
							requestDTO.getTargetMoney().subtract(item.getGradedCumulativeBegin()).multiply(new BigDecimal(item.getGradedCumulativeCost()));
					chargeFee = chargeFee.add(temp);
					sb.append(item.getGradedCumulativeBegin().stripTrailingZeros().toPlainString());
					sb.append("---");
					sb.append("∞");
					sb.append("：");
					sb.append("(");
//					sb.append(requestDTO.getTargetMoney().subtract(item.getGradedCumulativeBegin()).toPlainString());
					sb.append(requestDTO.getTargetMoney().toPlainString());
					sb.append("-");
					sb.append(item.getGradedCumulativeBegin().stripTrailingZeros().toPlainString());
					sb.append(")");
					sb.append("*");
					sb.append(item.getGradedCumulativeCost());
					sb.append("%%");
					sb.append("=");
					sb.append(temp.toPlainString());
					sb.append("元");
				} else {
					//否则就是该档位最大值和最小值的差值进行计算
					BigDecimal temp =
							item.getGradedCumulativeEnd().subtract(item.getGradedCumulativeBegin()).multiply(new BigDecimal(item.getGradedCumulativeCost()));
					chargeFee = chargeFee.add(temp);
					sb.append(item.getGradedCumulativeBegin().stripTrailingZeros().toPlainString());
					sb.append("---");
					sb.append(item.getGradedCumulativeEnd().stripTrailingZeros().toPlainString());
					sb.append("：");
					sb.append("(");
//					sb.append(item.getGradedCumulativeEnd().stripTrailingZeros().toPlainString());
					sb.append(item.getGradedCumulativeEnd().stripTrailingZeros().toPlainString());
					sb.append("-");
					sb.append(item.getGradedCumulativeBegin().stripTrailingZeros().toPlainString());
					sb.append(")");
					sb.append("x");
					sb.append(item.getGradedCumulativeCost());
					sb.append("%%");
					sb.append("=");
					sb.append(temp.toPlainString());
					sb.append("元");
					sb.append("\n");
				}
			} else {
				//此时没有最大值大，则金额减去最小值，然后乘以计算系数
				BigDecimal temp =
						requestDTO.getTargetMoney().subtract(item.getGradedCumulativeBegin()).multiply(new BigDecimal(item.getGradedCumulativeCost()));
				chargeFee = chargeFee.add(temp);
				sb.append(item.getGradedCumulativeBegin().stripTrailingZeros().toPlainString());
				sb.append("---");
				if (item.getGradedCumulativeEnd().compareTo(new BigDecimal("100001")) == 0) {
					sb.append("∞");
				} else {
					sb.append(item.getGradedCumulativeEnd().stripTrailingZeros().toPlainString());
				}
				sb.append("：");
				sb.append("(");
				sb.append(requestDTO.getTargetMoney().toPlainString());
				sb.append("-");
				sb.append(item.getGradedCumulativeBegin().stripTrailingZeros().toPlainString());
				sb.append(")");
				sb.append("x");
				sb.append(item.getGradedCumulativeCost());
				sb.append("%%");
				sb.append("=");
				sb.append(temp.toPlainString());
				sb.append("元");
				break;
			}
		}
		responseDTO.setChargeFee(chargeFee);
		responseDTO.setProcess(sb.toString());
		//判断如果吨数不为空，则计算计费数量
		if (ObjectUtil.isNotEmpty(requestDTO.getRebar())) {
			//先根据省份代码查询改省钢筋单价
			PmsSteelPrice pmsSteelPrice = steelPriceMapper.selectSteelPriceByAreaCode(requestDTO.getAreaCode());
			if (ObjectUtil.isNotEmpty(pmsSteelPrice)) {
				BigDecimal steelFee = requestDTO.getRebar().multiply(pmsSteelPrice.getSteelPrice());
				responseDTO.setSteelFee(steelFee);
			}
		}
		//计算效益费用
		if (ObjectUtil.isNotEmpty(requestDTO.getAuthorizeMoney()) && ObjectUtil.isNotEmpty(requestDTO.getSubmitMoney()) && StringUtils.isNotEmpty(requestDTO.getFeeRate())) {
			BigDecimal benefitFee =
					requestDTO.getAuthorizeMoney().subtract(requestDTO.getSubmitMoney()).multiply(new BigDecimal(requestDTO.getFeeRate())).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);
			responseDTO.setBenefitFee(benefitFee);
		}
		return CommonResult.success(responseDTO);
	}
}
